package qianfeng.com.meijiadaka.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.adapter.FIndAdapter;
import qianfeng.com.meijiadaka.adapter.FinishRefresh;
import qianfeng.com.meijiadaka.bean.FindBean;
import qianfeng.com.meijiadaka.interfacebean.FindData;
import qianfeng.com.meijiadaka.interfacebean.FindListData;
import qianfeng.com.meijiadaka.myview.MyLiveRecycleView;
import qianfeng.com.meijiadaka.myview.RecycleViewDivider;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewFragment extends Fragment {
    @BindView(R.id.find_rv)
    MyLiveRecycleView mFindRv;
    private  List<FindBean.ResultBean>mList;
    private String mBaseUrl="http://home.naildaka.com/";
    private String mPath = "http://home.naildaka.com/vapi/v4/home/picture/latest?uid=null&pageNo=";
    private int mPager=1;
    private FIndAdapter mFIndAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new, container, false);
        ButterKnife.bind(this, view);
        initUI();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadData();
    }

    private void initUI() {
        mList=new ArrayList<>();
        GridLayoutManager ma=new GridLayoutManager(getContext(),3, LinearLayoutManager.VERTICAL,false);
        mFindRv.getRefreshableView().setLayoutManager(ma);
        mFindRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(getContext(), LinearLayout.HORIZONTAL,3, Color.WHITE));
        mFindRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(getContext(), LinearLayout.VERTICAL,3, Color.WHITE));
        mFIndAdapter = new FIndAdapter(mList, getContext());
        mFindRv.setAdapter(mFIndAdapter);
        mFindRv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                mPager=1;
                loadData();
                new FinishRefresh(mFindRv,mFIndAdapter).execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                mPager++;
                loadData();
                new FinishRefresh(mFindRv,mFIndAdapter).execute();
            }
        });
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl(mBaseUrl).
                addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create()).build().create(FindData.class)
                .getData(mPath+mPager).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<FindBean>() {
            @Override
            public void call(FindBean findBean) {
                mList.addAll(findBean.getResult());
                mFIndAdapter.notifyDataSetChanged();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Toast.makeText(getContext(),"亲，网络不好",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
